package cn.nbt.busManage.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;

@org.springframework.context.annotation.Configuration
@MapperScan(basePackages = "cn.nbt.busManage.dao.mapper",sqlSessionFactoryRef = "sqlSessionFactoryMyBatis")
@EnableTransactionManagement
public class MybatisConfig {
    /**
     * SqlSessionFactoryBean配置
     * @return SqlSessionFactoryBean
     */
    @Bean(name = "sqlSessionFactoryMyBatis")
    public SqlSessionFactoryBean sqlSessionFactoryMyBatis(@Qualifier("mybatisDataSource") DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryMyBatis = new SqlSessionFactoryBean();
        sqlSessionFactoryMyBatis.setDataSource(dataSource);
        Configuration configuration = new Configuration();
        setting(configuration);
        sqlSessionFactoryMyBatis.setConfiguration(configuration);
        try {
            sqlSessionFactoryMyBatis.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath:mapper/*Mapper.xml"));
        }catch (IOException e){
            e.printStackTrace();
        }
        return sqlSessionFactoryMyBatis;
    }

    /**
     * 基础扫描包和加载sql工厂
     * @return MapperScannerConfigurer
     */
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("cn.nbt.busManage.repository.mapper");
        msc.setSqlSessionFactoryBeanName("sqlSessionFactoryMyBatis");
        return msc;
    }


    /**
     * mybatis配置
     * @param configuration 配置
     */
    private void setting(org.apache.ibatis.session.Configuration configuration) {
        //        不开启缓存
        configuration.setCacheEnabled(false);
        //        配置别名
        configuration.getTypeAliasRegistry().registerAliases("bean");
        //        开启驼峰命名
        configuration.setMapUnderscoreToCamelCase(true);
    }

    @Bean(name = "mybatisTransactionManager")
    public DataSourceTransactionManager myBatisTransactionManager(@Qualifier("mybatisDataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
